package com.bigkoo.svprogresshuddemo;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.bigkoo.svprogresshud.listener.OnDismissListener;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.window.dialog.ToastDialog;
import ohos.eventhandler.EventHandler;
import ohos.eventhandler.EventRunner;
import ohos.eventhandler.InnerEvent;
import ohos.multimodalinput.event.KeyEvent;

public class MainAbility extends Ability {
    private SVProgressHUD mSVProgressHUD;
    int progress = 0;
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_main);

        Button Button_show = (Button) findComponentById(ResourceTable.Id_Button_show);
        Button Button_showWithMaskType = (Button) findComponentById(ResourceTable.Id_Button_showWithMaskType);
        Button Button_showWithStatus = (Button) findComponentById(ResourceTable.Id_Button_showWithStatus);
        Button Button_showInfoWithStatus = (Button) findComponentById(ResourceTable.Id_Button_showInfoWithStatus);
        Button Button_showSuccessWithStatus = (Button) findComponentById(ResourceTable.Id_Button_showSuccessWithStatus);
        Button Button_showErrorWithStatus = (Button) findComponentById(ResourceTable.Id_Button_showErrorWithStatus);
        Button Button_showWithProgress = (Button) findComponentById(ResourceTable.Id_Button_showWithProgress);

        mSVProgressHUD = new SVProgressHUD(this, (ComponentContainer)findComponentById(ResourceTable.Id_layout_main));
        mSVProgressHUD.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(SVProgressHUD hud) {
                new ToastDialog(getContext()).setText("dismiss").show();
            }
        });

        Button_show.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                show();
            }
        });
        Button_showWithMaskType.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showWithMaskType();
            }
        });
        Button_showWithStatus.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showWithStatus();
            }
        });
        Button_showInfoWithStatus.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showInfoWithStatus();
            }
        });
        Button_showSuccessWithStatus.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showSuccessWithStatus();
            }
        });
        Button_showErrorWithStatus.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showErrorWithStatus();
            }
        });
        Button_showWithProgress.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                showWithProgress();
            }
        });
    }



    public void show(){
        mSVProgressHUD.show();
    }
    public void showWithMaskType(){
        mSVProgressHUD.showWithMaskType(SVProgressHUD.SVProgressHUDMaskType.None);
    }
    public void showWithStatus(){
        mSVProgressHUD.showWithStatus("加载中...");
    }
    public void showInfoWithStatus(){
        mSVProgressHUD.showInfoWithStatus("这是提示", SVProgressHUD.SVProgressHUDMaskType.None);
    }
    public void showSuccessWithStatus(){
        mSVProgressHUD.showSuccessWithStatus("恭喜，提交成功！");
    }
    public void showErrorWithStatus(){
        mSVProgressHUD.showErrorWithStatus("不约，叔叔我们不约～", SVProgressHUD.SVProgressHUDMaskType.GradientCancel);
    }


    private static final int thread_showprogress = 2;
    private static final int thread_keydown = 3;
    InnerEvent progress_InnerEvent = InnerEvent.get(thread_showprogress,0,null);
    InnerEvent keydown_InnerEvent = InnerEvent.get(thread_keydown);
    public EventHandler mHandler = new EventHandler(EventRunner.current()){
        @Override
        protected void processEvent(InnerEvent innerEvent){
            super.processEvent(innerEvent);
            showProgressBar();
        }
    };

    public void showProgressBar(){
        progress = progress + 5;
        if (mSVProgressHUD.getProgressBar().getMax() != mSVProgressHUD.getProgressBar().getProgress()) {
            mSVProgressHUD.getProgressBar().setProgress(progress);
            mSVProgressHUD.setText("进度 "+progress+"%");

            InnerEvent event = InnerEvent.get(thread_showprogress,0,null);
            mHandler.sendEvent(event,500);
        }
        else{
            mSVProgressHUD.dismiss();
        }
    }

    public void showWithProgress(){
        progress = 0;
        mSVProgressHUD.getProgressBar().setProgress(progress);//先重设了进度再显示，避免下次再show会先显示上一次的进度位置所以要先将进度归0
        mSVProgressHUD.showWithProgress("进度 " + progress + "%", SVProgressHUD.SVProgressHUDMaskType.Black);
        InnerEvent event = InnerEvent.get(thread_showprogress,0,null);
        mHandler.sendEvent(event,500);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        if (keyCode == KeyEvent.KEY_BACK )
        {
            if(mSVProgressHUD.isShowing()){
                mSVProgressHUD.dismiss();
                return true;
            }
        }

        return super.onKeyDown(keyCode, event);

    }
}
